#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2023/5/6 16:38
# @Author : 闫峰
# @File    : dev_multiprocessing_run.py
import concurrent.futures
import os
import subprocess
import time
from conf.settings import ensure_path_sep
from utils import config
from utils.notify.ding_talk import DingTalkSendMsg
from utils.notify.lark import FeiShuTalkChatBot
from utils.notify.send_mail import SendEmail
from utils.notify.wechat_send import WeChatSend
from utils.other_tools.allure_data.allure_report_data import AllureFileClean
from utils.other_tools.allure_data.error_case_excel import ErrorCaseExcel
from utils.other_tools.models import NotificationType
from utils.readFileUtils.get_all_files_path import get_all_executefile


env = 'beta08'

# 多进程执行pytest测试文件
def run_tests_with_multiprocessing(num_processes):
    with concurrent.futures.ProcessPoolExecutor(max_workers=num_processes) as executor:
        # submit()函数将任务提交到进程池中执行
        for filename in test_files:
            executor.submit(subprocess.run, ['pytest', '--alluredir', './report/tmp', filename])


# 同时启动3个进程执行pytest测试文件

if __name__ == '__main__':
    test_files = get_all_executefile(ensure_path_sep("\\test_case\\" + env), yaml_data_switch=True)
    start_time = time.time()
    run_tests_with_multiprocessing(8)
    os.system(r"allure generate ./report/tmp -o ./report/html --clean")
    # allure_data = AllureFileClean().get_case_count()
    # notification_mapping = {
    #     NotificationType.DING_TALK.value: DingTalkSendMsg(allure_data,env).send_ding_notification,
    #     NotificationType.WECHAT.value: WeChatSend(allure_data).send_wechat_notification,
    #     NotificationType.EMAIL.value: SendEmail(allure_data).send_main,
    #     NotificationType.FEI_SHU.value: FeiShuTalkChatBot(allure_data).post
    # }
    #
    # if config.notification_type != NotificationType.DEFAULT.value:
    #     notification_mapping.get(config.notification_type)()
    #
    # if config.excel_report:
    #     ErrorCaseExcel().write_case()
    # end_time = time.time()
    # print(f"消耗了{end_time - start_time}s")